<?php
namespace app\admin\logic;
use app\admin\controller\Upload;
use app\admin\model\Goods;
use think\facade\Request;
use think\facade\View;

class GoodsLogic
{



    public static function setSearchData($param)
    {
        $data = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $data['keywords'] = $param['keywords'];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $data['if_show'] = $param['if_show'][0];
        }
        if (isset($param['recommended']) && $param['recommended']<>''){
            $data['recommended'] = $param['recommended'];
        }
        if (isset($param['brand_id']) && !empty($param['brand_id'])){
            $data['brand_id'] = $param['brand_id'];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $data['cid'] = $param['cid'];
        }
        if (isset($param['start_time']) && !empty($param['start_time'])){
            $data['start_time'] = $param['start_time'];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])){
            $data['end_time'] = $param['end_time'];
        }
        return $data;
    }

    public static function changeFontColor($list,$param)
    {
        if (isset($param['keywords']) && !empty($param['keywords'])){
            foreach ($list as &$vo){
                $word = '<font color="red" size="5">'.$param['keywords'].'</font>';
                $vo['goods_name'] = str_replace($param['keywords'],$word,$vo['goods_name']);
            }
        }
        return $list;
    }

    public static function setGoodsOrder($param)
    {
        $order = ['goods_id'=>'desc'];
        View::assign(['msg'=>'', 'order'=>1]);
        if (isset($param['order']) && $param['order']==1){
            $order = ['add_time'=>'desc'];
            View::assign(['msg' => '↓', 'order' => 2]);
        }
        if (isset($param['order']) && $param['order']==2){
            $order = ['add_time'=>'asc'];
            View::assign(['msg' => '↑', 'order' => 0]);
        }
        return $order;
    }

    public static function setGoodsData($param)
    {
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
        ];

        View::assign($data);
        View::assign('data',$data);
    }

    public static function setGoodsWhere($param)
    {
        $where = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['goods_name','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $where[] = ['if_show','in',$param['if_show']];
        }
        if (isset($param['recommended']) && $param['recommended']<>''){
            $where[] = ['recommended','=',$param['recommended']];
        }
        if (isset($param['brand_id']) && !empty($param['brand_id'])){
            $where[] = ['brand','=',$param['brand_id']];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[] = ['cate_id','=',$param['cid']];
        }
        if (isset($param['start_time']) && !empty($param['start_time'])){
            $where[] = ['add_time','>',strtotime($param['start_time'])];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])){
            $where[] = ['add_time','<',strtotime($param['end_time'])];
        }
        return $where;
    }


    //商品添加
    public static function   GoodsAdd($post,$paths,$pathss){
        $if_show=isset($post['if_show'])?$post['if_show']:'';
        $recom=isset($post['recom'])?$post['recom']:'';
        $recommended=isset($post['recommended'])?$post['recommended']:'';
        $data=[
            'goods_name'=>$post['name'],
            'price'=>$post['price'],
            'cate_id'=>$post['cid'],
            'default_image'=>$paths,
            'banner_image'=>$pathss,
            'add_time'=>time(),
            'brand'=>$post['pinpai'],
            'vip'=>$post['vip_price'],
            'spec_qty'=>$post['num'],
            'recom'=>$recom,
            'recommended'=>$recommended,
            'if_show'=>$if_show,
            'description'=>$post['content'],
            'tags'=>$post['tags']
        ];
        return $data;

    }



    //商品修改
    public static function   goodsEditLogic($post,$path){
        $if_show=isset($post['if_show'])?$post['if_show']:'';
        $recom=isset($post['recom'])?$post['recom']:'';
        $recommended=isset($post['recommended'])?$post['recommended']:'';
        $data=[
            'goods_id'=>$post['goods_id'],
            'goods_name'=>$post['goods_name'],
            'price'=>$post['price'],
            'default_image'=>$path,
            'cate_id'=>$post['cate_id'],
            'brand'=>$post['brand'],
            'vip'=>$post['vip'],
            'spec_qty'=>$post['spec_qty'],
            'recom'=>$recom,
            'recommended'=>$recommended,
            'if_show'=>$if_show,
            'description'=>$post['description'],
        ];
        return $data;


    }

   //多图时的将临时路径与老路径到数据库
     public static function   getImgUrl($url){
         $post=Request::post();
          //$old_image 老路径
           $old_image=[];  $new_image=[];
         //$post['banner_image']=[];  $new_image=[];
         if (isset($post['new_imgs'])){
             foreach ($post['new_imgs'] as $v){
                 //取文件后缀
                 $s=explode('.',$v);
                 //正式路径
                 //目录
                 $path_date=$url.date('Y/m/d/');
                 //文件名+后缀
                 $path_dir=$path_date.md5(microtime(true).rand(100000,999999)).'.'.end($s);
                 $old_image[]=$path_dir;
                 //拼移动路径用的data
                 //临时路径与正式路径数组
                 $new_image[]=['new'=>$path_dir, 'old'=>$v,];
             }
         }
         if (isset($post['old_imgs'])){
             $old_image=array_merge($post['old_imgs'], $old_image);
         }
            return [$old_image,$new_image];
         }


         //单图上传老路径加新路径
     public static function   setImgUrl($default_image,$url){
             //取文件后缀
             $s=explode('.',$default_image);
             //正式路径
             //目录
             $path_date=$url.date('Y/m/d/');
             //文件名+后缀
             $path_dir=$path_date.md5(microtime(true).rand(100000,999999)).'.'.end($s);
            return [$path_date, $path_dir];
         }

     //多图  移动临时目录下图片到正式目录,并删除临时文件夹文件
    public static function   getImgDir($new_image,$url,$dir){
     //移动临时目录下图片到正式目录
     //判断是否存在文件夹
     $path_date=$url.date('Y/m/d/');
     if (!is_dir($dir.$path_date)){
         //参数1:文件名  参数2:权限  参数3:是否建立子文件夹
         mkdir($dir.$path_date,0777,true);
     }
     //循环拼接的临时路径与正式路径数组
     foreach ($new_image as $v){
         copy($dir.$v['old'],$dir.$v['new']);
         //删除临时目录文件
         unlink($dir.$v['old']);
     }




 }


 //单图 移动临时目录下图片到正式目录,并删除临时文件夹文件
     public static function   setImgDir($default_image,$path_date,$path_dir,$dir){

         //移动临时文件到正式目录
         if (!is_dir($dir.$path_date)){
             //参数1:文件名  参数2:权限  参数3:是否建立子文件夹
             mkdir($dir.$path_date,0777,true);
         }
         //将临时目录文件复制到正式文件中
         copy($dir.$default_image,$dir.$path_dir);
         unlink($dir.$default_image);

     }























    //goods分类
    public static function goodCate(){
        $cate=Goods::gcate();
        $fenlei=[];
        foreach ($cate as $v){
            if ($v['parent_id']==0){
                $fenlei[]=$v;
            }
        }
        foreach ($fenlei as &$v){
            foreach ($cate as $s){
                if ($s['parent_id']==$v['cate_id']){
                    $v['er'][]=$s;
                }
            }
            if (!isset($v['er'])){
                $v['er']=1;
            }
        }
        foreach ($fenlei as &$v){
            if (is_array($v['er'])){
                foreach ($v['er'] as &$vv){
                    foreach ($cate as $s){
                        if ($s['parent_id']==$vv['cate_id']){
                            $vv['san'][]=$s;
                        }
                    }
                    if (!isset($vv['san'])){
                        $vv['san']=1;
                    }
                }
            }
        }

        return $fenlei;
    }








}
